﻿using FineUI;
using System;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class ReportRecord : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        /// <summary>
        /// 申请流程环节是否已处理
        /// </summary>
        public bool IsHandle
        {
            get;
            set;
        }
        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
            if (IsHandle)
            { }
        }

        private void BindData()
        {
        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(this.CurrentStandID))
            {
                var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);

                if (oModel != null)
                {
                    IsHandle = oModel.AppllicationID == null;
                    if (IsHandle) return;
                    this.ddlReportRecord.SelectedValue = oModel.Status.ToString();
                }
            }

            //页面只读显示
            if (this.ReadOnly.Trim() == "1")
            {
                this.ddlReportRecord.Enabled = false;
                this.btnSubmit.Visible = false;

            }
        }



        private void SaveData(bool isClose, bool AdminSave = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过

            bool bResult = false;
            if (Convert.ToInt32(this.ddlReportRecord.SelectedValue) == (int)Enum_FlowStanceStatus.未处理 ||
                Convert.ToInt32(this.ddlReportRecord.SelectedValue) == (int)Enum_FlowStanceStatus.通过)
            { }
            else
            {
                var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                oModel.HandleUserID = this.CurrentUser.LoginID;
                oModel.HandleUserName = CurrentUser.UserName;
                oModel.HandleTime = DateTime.Now;
                oModel.Status = Convert.ToInt32(this.ddlReportRecord.SelectedValue);
                bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);

                PartyJoinChildFlow cfOmodel = new PartyJoinChildFlow();
                cfOmodel.StandID = CurrentStandID;
                cfOmodel.HandleUserID = CurrentUser.LoginID;
                cfOmodel.HandleUserName = CurrentUser.UserName;
                cfOmodel.HandleTime = DateTime.Now;
                cfOmodel.Status = Convert.ToInt32(this.ddlReportRecord.SelectedValue);
                PartyJoinChildFlow.HandleFlow(cfOmodel);
            }
            bResult = true;

            if (bResult)
            {
                if (AdminSave)
                    Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
                else
                    Alert.Show("提交成功", String.Empty, ActiveWindow.GetHideReference());
            }
            else
            {
                if (!string.IsNullOrEmpty(Message))
                    Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
            }
        }

        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.SaveData(true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        protected void btnReCell_Click(object sender, EventArgs e)
        {
            string tipMessage = "";
            PartyJoinLinkExample.ReLastStep(this.CurrentStandID, this.AppllicationID, out tipMessage,this.CurrentUser.LoginID);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}